<html>
<head><meta charset="utf-8"><title>Macro for repeating impls · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Macro.20for.20repeating.20impls.html">Macro for repeating impls</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="227236311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Macro%20for%20repeating%20impls/near/227236311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Macro.20for.20repeating.20impls.html#227236311">(Feb 22 2021 at 09:01)</a>:</h4>
<p>How would you write a macro that would allow to expand a single impl block to multiple impl blocks? I have methods that have the same implementation. I was thinking something along the lines of this, however I can't seem to be able to get pass the meta variables repeating different amounts. Is there a way to just tell a macro to repeat the whole meta-variable every time?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="fm">macro_rules!</span><span class="w"> </span><span class="n">shared_methods</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="k">impl</span><span class="w"> </span><span class="cp">$($types</span>:<span class="nc">ty</span><span class="p">),</span><span class="o">+</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cp">$($tree</span>:<span class="nc">tt</span><span class="p">)</span><span class="o">*</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="cp">$(</span><span class="w"></span>
<span class="w">            </span><span class="k">impl</span><span class="w"> </span><span class="cp">$types</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="cp">$($tree</span><span class="p">)</span><span class="o">*</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">)</span><span class="o">+</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">A</span><span class="p">;</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">B</span><span class="p">;</span><span class="w"></span>

<span class="n">shared_methods</span><span class="o">!</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">impl</span><span class="w"> </span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// Expands to</span>

<span class="k">impl</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="227238555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Macro%20for%20repeating%20impls/near/227238555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Macro.20for.20repeating.20impls.html#227238555">(Feb 22 2021 at 09:21)</a>:</h4>
<p>I think this should do the trick?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="fm">macro_rules!</span><span class="w"> </span><span class="n">shared_methods</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="k">impl</span><span class="w"> </span><span class="cp">$type</span>:<span class="nc">ty</span><span class="w"> </span><span class="cp">$(,$types</span>:<span class="nc">ty</span><span class="p">)</span><span class="o">*</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cp">$($tree</span>:<span class="nc">tt</span><span class="p">)</span><span class="o">*</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">impl</span><span class="w"> </span><span class="cp">$type</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="cp">$($tree</span><span class="p">)</span><span class="o">*</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="n">shared_methods</span><span class="o">!</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">impl</span><span class="w"> </span><span class="cp">$($types</span><span class="p">),</span><span class="o">*</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cp">$($tree</span><span class="p">)</span><span class="o">*</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="k">impl</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cp">$($tree</span>:<span class="nc">tt</span><span class="p">)</span><span class="o">*</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">A</span><span class="p">;</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">B</span><span class="p">;</span><span class="w"></span>

<span class="n">shared_methods</span><span class="o">!</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">impl</span><span class="w"> </span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="227238727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Macro%20for%20repeating%20impls/near/227238727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Macro.20for.20repeating.20impls.html#227238727">(Feb 22 2021 at 09:23)</a>:</h4>
<p>This works:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="fm">macro_rules!</span><span class="w"> </span><span class="n">shared_methods</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="k">impl</span><span class="w"> </span><span class="cp">$($types</span>:<span class="nc">ty</span><span class="p">),</span><span class="o">+</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cp">$($tree</span>:<span class="nc">tt</span><span class="p">)</span><span class="o">*</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">shared_methods</span><span class="o">!</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">@</span><span class="k">impl</span><span class="w"> </span><span class="cp">$($types</span><span class="p">),</span><span class="o">+</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cp">$($tree</span><span class="p">)</span><span class="o">*</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="o">@</span><span class="k">impl</span><span class="w"> </span><span class="cp">$($types</span>:<span class="nc">ty</span><span class="p">),</span><span class="o">+</span><span class="w"> </span><span class="p">{</span><span class="cp">$tree</span>:<span class="nc">tt</span><span class="p">})</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="cp">$(</span><span class="k">impl</span><span class="w"> </span><span class="cp">$types</span><span class="w"> </span><span class="cp">$tree</span><span class="p">)</span><span class="o">+</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="227238974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Macro%20for%20repeating%20impls/near/227238974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Macro.20for.20repeating.20impls.html#227238974">(Feb 22 2021 at 09:25)</a>:</h4>
<p>Thank you both, I had a feeling there was a way to do it with a recurisive macro but wasn't sure of what was needed.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>